package com.zmq.controller;

import com.alibaba.fastjson.JSON;
import com.zmq.bean.UserBean;
import com.zmq.service.user.IUserService;
import com.zmq.utils.redis.IRedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IRedisService redisService;

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);
    /**
     * 添加用户
     * @param userBean
     * @return
     */
    @RequestMapping("addUser")
    @ResponseBody
    public Map<String,Object> addUser(UserBean userBean){
        Map map = new HashMap();
        if(userBean == null){
            map.put("code", false);
            map.put("msg", "不能为空！");
        }
        String userid = userService.addUser(userBean);
        map.put("code", true);
        map.put("msg", "添加成功！账号："+userBean.getUsername());
        return map;
    }

    /**
     * 删除用户
     * @param userBean
     * @return
     */
    @RequestMapping("deleteUser")
    @ResponseBody
    public Map<String,Object> deleteUser(UserBean userBean){
        Map map = new HashMap();
        if(userBean == null){
            map.put("code", false);
            map.put("msg", "不能为空！");
        }
        userService.deleteUser(userBean);
        return map;
    }

    /**
     * 修改用户
     * @param userBean
     * @return
     */
    @RequestMapping("updateUser")
    @ResponseBody
    public Map<String,Object> updateUser(UserBean userBean){
        Map map = new HashMap();
        if(userBean == null){
            map.put("code", false);
            map.put("msg", "不能为空！");
        }
        userService.updateUser(userBean);
        return map;
    }

    /**
     * 查询用户
     * @return
     */
    @RequestMapping("queryUserById")
    @ResponseBody
    public Map<String,Object> queryUserById(String id){
        Map map = new HashMap();

        userService.queryUserById(id);
        return map;
    }

    /**
     * 从redis中查询用户信息
     * @return
     */
    @RequestMapping("showUser")
    @ResponseBody
    public Map queryUserFromRedis(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Map map = new HashMap();
        String sessionID = "";

        //获取cookie
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            response.sendRedirect("/index.html");
            map.put("code", false);
            map.put("msg", "error");
            return map;
        }
        //从Cookies中获取自己想要的cookie
        for (Cookie cookie : cookies) {
            if ("COOKIE_SESSION".equals(cookie.getName())) {
                sessionID = cookie.getValue().toString();
            }
        }
        //cookie不能是空
        if ("".equals(sessionID)) {
            response.sendRedirect("/index.html");
            map.put("code", false);
            map.put("msg", "未获取到cookie，请设置允许本网站取得cookie!");
            return map;
        }
        //根据获取到的sessionId获取session信息
        UserBean user = (UserBean) redisService.get(sessionID);
        user.setUsername("");
        user.setPassword("");
        user.setRoleId("");
        String s = JSON.toJSONString(user);
        //System.out.println(s);
        map.put("code", true);
        map.put("data", s);
        return map;
    }
}
